
package com.zizy.healthexpert.Database;


import android.content.Context;
import android.util.Log;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS.T_FOODS;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class DatabaseUtil {
	private Context context;
	public final boolean DISHES= true ;
	public final boolean MATERIAL =false;
	public DatabaseUtil( Context context) {
		 this.context=context;
		 
	}

	public class Nutrtion {
		public String name;
		public double nutrtionValue;
		public String unit;
		
		public Nutrtion(String name, double nutrtionValue, String unit) {
			this.name = name;
			this.nutrtionValue = nutrtionValue;
			this.unit = unit;

		}
		
	}
	public class Dishes{
		ArrayList<String> mainIngredient =new ArrayList<String>() ;
		ArrayList<String> minorIngredient=new ArrayList<String>();
		
	}
	public Dishes SearchDishes(String foodName){
		//create table QAQ
		
		//search QAQ
		
		return null;
	}
	
	public ArrayList<Nutrtion> SearchNutrition(String foodName) {
		ArrayList<Nutrtion> findNutrtions = new ArrayList<DatabaseUtil.Nutrtion>();

		int rec = 0;
		String nutritionName = "";
		double nutritionValue = 0;
		String unit = ""; // 單位
		MyDataBase_T_FOODS tableFoods = new MyDataBase_T_FOODS(context);

		ArrayList<MyDataBase_T_FOODS.T_FOODS> foods = tableFoods.SearchT_FOODS(
				"*", "NAME='" + foodName + "'");

		for (T_FOODS t_FOODS : foods) {

			Log.i("Log on create", String.valueOf(t_FOODS.FOOD_TYPE));
		}
		MyDataBase_T_NUTRITION tableNutrition = new MyDataBase_T_NUTRITION(
				context);
		ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> foods2 = tableNutrition
				.SearchT_NUTRITION("*", "RecNo=" + rec);
		for (MyDataBase_T_NUTRITION.T_NUTRITION t_FOODS : foods2) {
			nutritionName = String.valueOf((t_FOODS.NAME));
			unit = t_FOODS.UNIT;
		}

		MyDataBase_T_FOODS_NUTRITION tableFoodsNutrition = new MyDataBase_T_FOODS_NUTRITION(
				context);
		ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> foods3 = tableFoodsNutrition
				.SearchT_FOODS_NUTRITION("*", "RecNo=" + rec);
		for (MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION t_FOODS : foods3) {
			nutritionValue = t_FOODS.CONENT_VALUE;
			findNutrtions
					.add(new Nutrtion(nutritionName, nutritionValue, unit));

		}

		tableFoods.Close();
		tableFoodsNutrition.Close();
		tableNutrition.Close();
		return findNutrtions;

	}
	
	// calculate ,and return a hashmap,shows nutrition's value,if not give a right amount return null.
	public LinkedHashMap<Nutrtion ,Integer> CalculateNutritionValue(String foodName,String amount){
		//create table 
		
		//find the food
		//if the amount is united g.
			//find all nutrtion(SearchNutrition())
			//return
		
		
		return null;
	}
	
	/* 
	 * find is the food suitable to eat
	 */
	public int FindIsSuitableToEat(String foodName,int idForWho ){//0:suitable 1:not suitable 2:can eat some
		//Create table personalInf
		MyDatabase_PersonalInf tablePersonalInf=new MyDatabase_PersonalInf(context);		
		//Get personal inf(what disease)
		ArrayList<Object> disease=null;//not it's not a list, single row
		disease=tablePersonalInf.SearchDatabase("DiseaseName","ID="+idForWho);
		//Format Disease
		MyDatabase_PersonalInf.FieldsPersonalInf fieldsPersonalInf=(MyDatabase_PersonalInf.FieldsPersonalInf)disease.get(0);
		String diseaseNames=fieldsPersonalInf.diseaseName;
		String[] diseaseArray=diseaseNames.split(",");
		ArrayList<Integer> diseaseIDsList=new ArrayList<Integer>();
		for (String string : diseaseArray) {
			if (string.matches("[0-9]+")){ 							 
				diseaseIDsList.add(Integer.parseInt(string));
				
			}
		}
		//Create Table Disease
		MyDataBase_Disease tableDisease=new MyDataBase_Disease(context);
		//Find all foods suitable/not suitable
		
		//Find is this food suitable to eat
		return 0;
	}
 
	/* 
	 * search food(material and dishes) in database, use in SearchActivity
	 */
	public ArrayList<FoodResult> Search(String foodName){
		ArrayList<FoodResult> foodResultsList=new ArrayList<DatabaseUtil.FoodResult>();
		
		ArrayList<FoodResult> findFoodList=new ArrayList<DatabaseUtil.FoodResult>();	
		//create table material 
		MyDataBase_T_FOODS myDataBaseFoods=new MyDataBase_T_FOODS(context);
		//search material
		ArrayList<T_FOODS> resultMaterial=myDataBaseFoods.SearchT_FOODS("*","NAME like %" +foodName+"%");
		//create table dishes
		MyDatabase_Dishes myDatabase_Dishes=new MyDatabase_Dishes(context);
		//search dishes
		ArrayList<Object> resultDishes=myDatabase_Dishes.SearchDatabase("*", "DishesName like %" +foodName+"%");
		//merge two result
		for (T_FOODS object : resultMaterial) {
			T_FOODS result=object;
			foodResultsList.add(new FoodResult(result.NAME, MATERIAL, result._ID));	
		}
		for (Object object : resultDishes) {
			MyDatabase_Dishes.FieldsDishes result=(MyDatabase_Dishes.FieldsDishes)object;
			foodResultsList.add(new FoodResult(result.dishesName, DISHES, result.dishesID));	
		}
		//return result
		return findFoodList;
	}
	
	public class  FoodResult{
		String foodName;
		boolean foodType; //true : Dishes;false:Material
		int foodId;
		
		public FoodResult(String foodName,boolean foodType,int foodId) {
			this.foodId=foodId;
			this.foodType=foodType;
			this.foodName=foodName;	
		}
		
	}
	
	
	
}
